<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2022-08-25 21:34:16
 * @LastEditors: CHINAKAIYUAN
 * @Description:
 */

namespace App\Repositories;

use App\Models\OpenCardDetails;

class OpenCardDetailsRepository extends Repositories
    {
    public $relation_models = [
        "material",
        "creator",
        "wip",
        'salesman',
        'process_pass' => ['process', 'swipor', "direction"],
        'customer',
        'process',
        'order',
        'processrename',
        'main'         => [
            'process_route' => ['details' => ['process']],
            'status',
            'bom',
            'raws'          => [
                'glue',
                'material',
                'newmaterial',
                'point_stick'
            ]
        ],
        'unit',
        'storageauditor',
        'storagestatus',
        'machine'      => ['employee'],
        'washed_index',
        'strip_index',
        'inspect',
        'teststatus',
        'testor',
        'cancel',
        'pass_last'
    ];
    public function boot(OpenCardDetails $model)
        {
        $this->model = $model;
        }
    public function audit($v)
        {
        if ($v->storagestatus_id == 1) {
            fail("单据{$v['code']}{$v->storagestatus->name}，出库失败");
            }
        $v['storagestatus_id']  = 1;
        $v['storageauditor_id'] = auth()->user()->employee_id;
        $v['storageaudited_at'] = date("Y-m-d H:i:s");
        return $v->subStorage()->save();
        }
    public function noaudit($v)
        {
        if ($v->storagestatus_id == 0) {
            fail("单据{$v['code']}{$v->storagestatus->name}，取消出库失败");
            }
        $v['storagestatus_id']  = $v['storageauditor_id'] = 0;
        $v['storageaudited_at'] = null;
        return $v->nosubStorage()->save();
        }
  
    }
